package Test;

/**
 * 给定一个链表，返回链表开始入环的第一个节点。 如果链表无环，则返回 NULL
 */
public class Test07 {
    public MySingleList.ListNode detectCycle(MySingleList.ListNode head) {
            MySingleList.ListNode fast = head;
            MySingleList.ListNode slow = head;
            while (fast != null && fast.next != null) {
                fast = fast.next.next;
                slow = slow.next;
                if(fast == slow) {
                    break;
                }
            }
            if(fast == null || fast.next == null) {
                return null;
            }
            //
            slow = head;
            while (fast != slow) {
                fast = fast.next;
                slow = slow.next;
            }
            return fast;
        }

}

